package registry

import (
	"context"
	. "gitee.com/aldrice/monitor/configs"
	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
	"github.com/influxdata/influxdb-client-go/v2/api"
	"time"
)

var client influxdb2.Client
var writeAPI api.WriteAPIBlocking
var queryAPI api.QueryAPI

func InitInfluxdb() error {
	option := influxdb2.DefaultOptions()
	option.SetPrecision(time.Second)
	client = influxdb2.NewClientWithOptions(
		Config.InfluxDB.ServerURL,
		Config.InfluxDB.AuthToken,
		option,
	)
	_, err := client.Health(context.Background())
	if err != nil {
		return err
	}
	writeAPI = client.WriteAPIBlocking(Config.InfluxDB.Org, "monitor_backend")
	queryAPI = client.QueryAPI(Config.InfluxDB.Org)
	return nil
}

func GetClient() *influxdb2.Client {
	return &client
}

// GetWriteAPI 获取写入对象
func GetWriteAPI() *api.WriteAPIBlocking {
	return &writeAPI
}

// GetQueryAPI 获取读取对象
func GetQueryAPI() *api.QueryAPI {
	return &queryAPI
}
